package com.mhkj.execute.plan.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.execute.plan.entity.PlanNodeNew;
import com.mhkj.execute.plan.service.PlanNodeNewService;
import com.mhkj.revise.plan.entity.Plan;
import com.mhkj.revise.plan.service.PlanService;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

@Controller
@RequestMapping("/planExe")
public class PlanController extends BaseController<Plan> {
	
	@Resource
	private PlanService planService;

	@Resource
	private PlanNodeNewService planNodeNewService;
	@Resource
	private DictionaryService dictionaryService;
	
	@Resource
	private JsonHandler jsonHandler;

	
	/**
	 * 计划详情
	 */
	@RequestMapping("/planList")
	@ResponseBody
	public List<Map<String, Object>> planList(HttpServletResponse response,long planTypeId){
		List<Plan> listPlan = planService.findPlanByTypeId(planTypeId);
		List<Map<String, Object>> list= new ArrayList<>();
		for (Plan plan:listPlan) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", plan.getId());
			map.put("name", plan.getName());
			list.add(map);
		}
		/*
		jsonHandler.sendJsonString(response, true, "获取数据成功", listPlan, new String[]{"id","name","state","auditRemark","auditDate"}, "id","name","state","auditRemark","auditDate");*/
		return list;
	}
	
	/**
	 * 新增工作计划
	 * @param response
	 * @param planNode
	 * @param pageIndex
	 * @param pageSize
	 * @throws ParseException 
	 */
	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response,String yearMonth,PlanNodeNew planNode,Long itemId,int pageIndex,int pageSize) throws ParseException {
		planNode.setSign("new");
		
		String firstDay="";
		String lastDay="";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		if(yearMonth!=null) {

			Date formattime = new SimpleDateFormat("yyyy-MM").parse(yearMonth);

			firstDay=format.format(formattime.getTime());
		    Calendar calendar = Calendar.getInstance();
		    calendar.setTime(formattime);
		    calendar.set(Calendar.DAY_OF_MONTH, 1); 
		    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
			lastDay=format.format(calendar.getTime());
		}
		Pagination<PlanNodeNew> pagination = planNodeNewService.newNodelistByPage(planNode,firstDay,lastDay,itemId, pageIndex, pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,
				new String[] { "id","plan.name","business.typeName","plan.planType.value","nodeName","nodeProperty.value","completeStandard","rate","state","beginDate","endDate","postPersonName","remark","createDate","auditState","auditResult","auditDate" },
				new String[] { "id","planName","businessName","planTypeName","nodeName","nodePropertyName","completeStandard","rate","state","beginDate","endDate","postName","remark","createDate","auditState","auditResult","auditDate" });
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMessage edit(PlanNodeNew t) {
		PlanNodeNew planNodeNew = planNodeNewService.getById(t.getId());
		if(planNodeNew.getAuditState()==0) {
			planNodeNew.setAuditState(1);
		}
		String message =planNodeNewService.edit(planNodeNew);
		return ResponseMessage.createMessage(true, message);
	}
	
	
	@RequestMapping("/getByCode")
	@ResponseBody
	public List<Dictionary> getByCode(String code){
		List<Dictionary> list = new ArrayList<>();
		list.add(dictionaryService.getByCode(code));
		return list;
	}
}
